﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InternetShop.Code;
using System.Web.Profile;

namespace InternetShop
{
    public partial class OrderPage : System.Web.UI.Page
    {
        //public override object this[string propertyName] {get; set; }

        protected Basket CurrentBasket
        {
            get
            {
                var b = (Basket)Session["Basket"];
                if (b == null)
                {
                    if (string.IsNullOrEmpty(Context.Profile.UserName))
                        b = new Basket();
                    else
                        b = Basket.LoadForUser(Context.Profile.UserName);
                    Session["Basket"] = b;
                }
                return b;
            }
        }

        protected string GeneratePath(object obj)
        {
            Product prod = obj as Product;
            return string.IsNullOrEmpty(prod.ImagePath) ? ResolveUrl("~/ContentImages/noImage.jpg") : ResolveUrl("~/ContentImages/" + prod.ImagePath);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            address.Text = (String)Context.Profile["Address"];
            rOrder.DataSource = CurrentBasket.Goods;
            rOrder.DataBind();
        }

        protected string getCost()
        {
            decimal price = 0;
            foreach (Product i in CurrentBasket.Goods)
            {
                price += (decimal)i.Price;
            }

            return price.ToString();
        }

        protected void onClick(Object sender, EventArgs e)
        {
            if(decimal.Parse(getCost()) != 0)
                isComplete.Text = "Order is complete!";
            else
                isComplete.Text = "Error! Basket is empty!";
            CurrentBasket.Clear();
            Context.Profile["Address"] = address.Text;
            Context.Profile.Save();
            Page.DataBind();
            address.Visible = false;
            addLab.Visible = false;
            rOrder.Visible = false;
            orderSend.Visible = false;
        }

        protected void redirect(Object sender, EventArgs e)
        {
            Response.Redirect("~/");
        }
    }
}